package cn.edu.jxust.controller.api.enterprise;

import cn.edu.jxust.common.domain.entity.PollutionSourceType;
import cn.edu.jxust.common.domain.web.api.enterprise.IPollutionSourceTypeController;
import cn.edu.jxust.common.response.Result;
import cn.edu.jxust.common.response.ResultUtils;
import cn.edu.jxust.common.response.enums.ResultStatus;
import cn.edu.jxust.core.domain.repository.PollutionsourcetypeService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhouzhicheng
 * @since 2020-06-29
 */
@Slf4j
@RestController
public class PollutionSourceTypeController implements IPollutionSourceTypeController {

    @Resource
    private PollutionsourcetypeService pollutionsourcetypeService;

    @Override
    public Result getAllPollutionSourceType(Integer page, Integer size){

        try{
            IPage<PollutionSourceType> pageInfo = pollutionsourcetypeService.page(new Page<>(page,size));
            if (pageInfo.getRecords() != null && !pageInfo.getRecords().isEmpty()){
                return ResultUtils.resultWithData(ResultStatus.SUCCESS, pageInfo);
            }else {
                return ResultUtils.resultWithoutData(ResultStatus.NOT_FOUND, "未找到任何关于污染源类型信息");
            }
        }catch (Exception e){
            log.error("PollutionSourceTypeController --> getAllPollutionSourceType: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result getOnePollutionSourceType(Integer id){

        try{
            PollutionSourceType pollutionsourcetype = pollutionsourcetypeService.getById(id);
            if(pollutionsourcetype != null){
                return ResultUtils.resultWithData(ResultStatus.SUCCESS, pollutionsourcetype);
            }else {
                return ResultUtils.resultWithoutData(ResultStatus.NOT_FOUND, "未找到任何关于污染源类型信息");
            }
        }catch (Exception e){
            log.error("PollutionSourceTypeController --> getOnePollutionSourceType: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result addOnePollutionSourceType(PollutionSourceType pollutionsourcetype, BindingResult result){

        if(result.hasErrors()){
            return ResultUtils.resultWithoutData(ResultStatus.PARAM_ERROR);
        }
        try{
            return pollutionsourcetypeService.save(pollutionsourcetype)
                    ? ResultUtils.resultWithoutData(ResultStatus.SUCCESS, "添加成功")
                    : ResultUtils.resultWithoutData(ResultStatus.FAILED, "添加失败, 请重试!");
        }catch (Exception e){
            log.error("PollutionSourceTypeController --> setOnePollutionSourceType: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result updateOnePollutionSourceType(PollutionSourceType pollutionsourcetype, BindingResult result){

        if(result.hasErrors()){
            return ResultUtils.resultWithoutData(ResultStatus.PARAM_ERROR);
        }
        try {
            return pollutionsourcetypeService.update(pollutionsourcetype,new QueryWrapper<PollutionSourceType>().eq("id",pollutionsourcetype.getId()))
                    ? ResultUtils.resultWithoutData(ResultStatus.SUCCESS,"修改成功！")
                    : ResultUtils.resultWithoutData(ResultStatus.FAILED, "修改失败");
        }catch (Exception e){
            log.error("PollutionSourceTypeController --> updateOnePollutionSourceType: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }

    @Override
    public Result deleteOnePollutionSourceType(Integer id) {

        try {
            return pollutionsourcetypeService.removeById(id)
                    ? ResultUtils.resultWithoutData(ResultStatus.SUCCESS, "删除成功")
                    : ResultUtils.resultWithoutData(ResultStatus.FAILED, "删除失败");
        }catch (Exception e){
            log.error("PollutionSourceTypeController --> deleteOnePollutionSourceType: {}", e.getMessage());
            return ResultUtils.resultWithoutData(ResultStatus.FAILED);
        }

    }
}

